import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

class Admin extends User{
    private Map<String, User> users;
    private List<Movie> movies;
    private List<Screening>screenings;
    private List<Ticket>tickets;

    public Admin(String userId, String username, String password, String phone,
                 Map<String, User> users, List<Movie> movies, List<Screening> screenings, List<Ticket> tickets) {
        super(userId, username, password, UserType.ADMIN, phone);
        this.users = users;
        this.movies = movies;
        this.screenings = screenings;
        this.tickets = tickets;
    }

    @Override
    public void menu(){
        while(true){
        System.out.println("\n===== 管理员菜单 =====");
        System.out.println("1. 密码管理");
        System.out.println("2. 用户管理");
        System.out.println("3. 退出登录");
        System.out.print("请选择: ");

        try {
            int choice=Integer.parseInt(scanner.nextLine());
            switch(choice){
                case 1:
                    passwordManagement();
                    break;
                case 2:
                    userManagement();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择");
            }
        } catch (NumberFormatException e) {
            System.out.println("请输入有效数字");
        }
      }
    }

    // 密码管理
    private void passwordManagement() {
        System.out.println("\n===== 密码管理 =====");
        System.out.println("1. 修改自身密码");
        System.out.println("2. 重置其他用户密码");
        System.out.println("3. 退出");
        System.out.print("请选择: ");

        try {
            int choice = Integer.parseInt(scanner.nextLine());
            switch (choice) {
                case 1:
                    changeOwnPassword();
                    break;
                case 2:
                    resetOtherPassword();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择");
            }
        } catch (NumberFormatException e) {
            System.out.println("请输入有效数字");
        }
    }

    // 重置其他用户密码
    private void resetOtherPassword() {
        System.out.print("请输入要重置密码的用户ID: ");
        String userId = scanner.nextLine();

        User user = users.get(userId);
        if (user == null) {
            System.out.println("用户不存在");
            return;
        }

        if (user.getUserType() == UserType.ADMIN && user.getUserId().equals("admin001")) {
            System.out.println("不能重置admin管理员密码");
            return;
        }

        user.password = "123456";
        System.out.println("用户 " + user.getUsername() + " 的密码已重置为 123456");
        Logger.log("重置用户密码: " + user.getUsername(), this.getUsername());
    }

    // 用户管理
    private void userManagement() {
        while(true) {
            System.out.println("\n===== 用户管理 =====");
            System.out.println("1. 列出所有用户");
            System.out.println("2. 添加用户");
            System.out.println("3. 删除用户");
            System.out.println("4. 查询用户");
            System.out.println("5. 修改用户信息");
            System.out.println("6. 退出");
            System.out.print("请选择: ");

            try {
                int choice = Integer.parseInt(scanner.nextLine());
                switch (choice) {
                    case 1:
                        listAllUsers();
                        break;
                    case 2:
                        addUser();
                        break;
                    case 3:
                        deleteUser();
                        break;
                    case 4:
                        searchUser();
                        break;
                    case 5:
                        updateUser();
                        break;
                    case 6:
                        return;
                    default:
                        System.out.println("无效选择");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效数字");
            }
        }
    }

    // 列出所有用户
    private void listAllUsers() {
        System.out.println("\n===== 用户列表 =====");
        System.out.printf("%-12s %-15s %-20s %-10s %-15s%n",
                "用户ID", "用户名", "注册时间", "用户类型", "手机号");

        for (User user : users.values()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.printf("%-12s %-15s %-20s %-10s %-15s%n",
                    user.getUserId(),
                    user.getUsername(),
                    sdf.format(user.getRegisterTime()),
                    user.getUserType().name(),
                    user.getPhone());
        }
    }

    // 添加用户
    private void addUser() {
        System.out.println("\n===== 添加用户 =====");
        System.out.print("用户ID: ");
        String userId = scanner.nextLine();

        if (users.containsKey(userId)) {
            System.out.println("用户ID已存在");
            return;
        }

        System.out.print("用户名: ");
        String username = scanner.nextLine();

        for (User user : users.values()) {
            if (user.getUsername().equals(username)) {
                System.out.println("用户名已存在");
                return;
            }
        }

        System.out.print("手机号: ");
        String phone = scanner.nextLine();

        for (User user : users.values()) {
            if (user.getPhone().equals(phone)) {
                System.out.println("手机号已存在");
                return;
            }
        }

        System.out.println("用户类型(1.管理员 2.经理 3.前台):");
        int typeChoice=Integer.parseInt(scanner.nextLine());
        UserType userType=null;
        switch (typeChoice) {
            case 1:
                userType = UserType.ADMIN;
                break;
            case 2:
                userType = UserType.MANAGER;
                break;
            case 3:
                userType = UserType.FRONT_DESK;
                break;
            default:
                System.out.println("无效选择");
                return;
        }

        User newUser;
        switch (userType) {
            case ADMIN:
                newUser = new Admin(userId,username,"123456",phone,users,movies,screenings,tickets);
                break;
            case MANAGER:
                newUser = new Manager(userId,username,"123456",phone,users,movies,screenings,tickets);
                break;
            case FRONT_DESK:
                newUser = new FrontDesk(userId,username,"123456",phone,screenings,movies,tickets);
                break;
            default:
                return;
        }

        users.put(userId,newUser);
        System.out.println("用户添加成功");
        Logger.log("添加用户:"+username,this.getUsername());
    }

    // 删除用户
    private void deleteUser() {
        System.out.println("\n===== 删除用户 =====");
        System.out.print("请输入要删除的用户ID: ");
        String userId = scanner.nextLine();

        if (!users.containsKey(userId)) {
            System.out.println("用户不存在");
            return;
        }

        User userToDelete = users.get(userId);
        if (userToDelete.getUserId().equals("admin001")) {
            System.out.println("不能删除admin管理员");
            return;
        }

        System.out.print("确定要删除用户 " + userToDelete.getUsername() + " 吗? (y/n): ");
        String confirm = scanner.nextLine();
        if (confirm.equalsIgnoreCase("y")) {
            users.remove(userId);
            System.out.println("用户删除成功");
            Logger.log("删除用户: " + userToDelete.getUsername(), this.getUsername());
        } else {
            System.out.println("取消删除");
        }
    }

    // 查询用户
    private void searchUser() {
        System.out.println("\n===== 查询用户 =====");
        System.out.print("请输入用户ID或用户名: ");
        String keyword = scanner.nextLine();
        System.out.printf("%-12s %-15s %-20s %-10s %-15s%n",
                "用户ID", "用户名", "注册时间", "用户类型", "手机号");

        for (User user : users.values()) {
            if (user.getUserId().contains(keyword) || user.getUsername().contains(keyword)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.printf("%-12s %-15s %-20s %-10s %-15s%n",
                        user.getUserId(),
                        user.getUsername(),
                        sdf.format(user.getRegisterTime()),
                        user.getUserType().name(),
                        user.getPhone());
            }
        }
    }

    // 修改用户信息
    private void updateUser() {
        System.out.println("\n===== 修改用户信息 =====");
        System.out.print("请输入要修改的用户ID: ");
        String userId = scanner.nextLine();

        User userToUpdate = users.get(userId);
        if (userToUpdate == null) {
            System.out.println("用户不存在");
            return;
        }

        if (userToUpdate.getUserType() == UserType.ADMIN) {
            System.out.println("不能修改管理员信息");
            return;
        }

        System.out.println("当前信息:");
        System.out.println("用户类型: " + userToUpdate.getUserType());
        System.out.println("手机号: " + userToUpdate.getPhone());

        System.out.println("修改用户类型 (1.经理 2.前台): ");
        int typeChoice = Integer.parseInt(scanner.nextLine());
        switch (typeChoice) {
            case 1:
                if (userToUpdate.getUserType() != UserType.MANAGER) {
                    Manager newManager = new Manager(
                            userToUpdate.getUserId(),
                            userToUpdate.getUsername(),
                            userToUpdate.password,
                            userToUpdate.getPhone(),
                            users,movies,screenings,tickets
                    );
                    users.put(userId, newManager);
                    System.out.println("用户类型已修改为经理");
                }
                break;
            case 2:
                if (userToUpdate.getUserType() != UserType.FRONT_DESK) {
                    FrontDesk newFrontDesk = new FrontDesk(
                            userToUpdate.getUserId(),
                            userToUpdate.getUsername(),
                            userToUpdate.password,
                            userToUpdate.getPhone(),
                           screenings,movies,tickets
                    );
                    users.put(userId, newFrontDesk);
                    System.out.println("用户类型已修改为前台");
                }
                break;
            default:
                System.out.println("无效选择，用户类型未修改");
        }

        System.out.print("请输入新手机号: ");
        String newPhone = scanner.nextLine();
        for (User user : users.values()) {
            if (user != userToUpdate && user.getPhone().equals(newPhone)) {
                System.out.println("手机号已存在");
                return;
            }
        }

        userToUpdate.phone = newPhone;
        System.out.println("手机号修改成功");
        Logger.log("修改用户信息: " + userToUpdate.getUsername(), this.getUsername());
    }
}